<?php
/**
* @package plugin itemtype for Pages-and-Items
* @version 1.0.4
* @copyright Copyright (C) 2006-2008 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

global $database;
	
//get database
if( defined('_JEXEC') ){
	//joomla 1.5
	$database = JFactory::getDBO();
}	
	
//get data from sub_item
$database->setQuery("SELECT url, size FROM #__pi_subitem_youtube WHERE item_id='$id' LIMIT 1");
$rows = $database->loadObjectList();
$sub_item_row = $rows[0];
$sub_item_url = trim($sub_item_row->url);
$sub_item_size = trim($sub_item_row->size);


//get videoid out of url
$pos_v = strpos($sub_item_url, 'watch?v=');
$string_lenght = strlen($sub_item_url);
$temp = substr($sub_item_url, $pos_v+8, $string_lenght);
$pos_and = strpos($temp, '&');
if(!$pos_and){
	$pos_and = strlen($temp);
}
$video_id = substr($sub_item_url, $pos_v+8, $pos_and);

//get sizes
$sizes = explode('x',$sub_item_size);
$width = $sizes[0];
$height = $sizes[1];


//only output when there is a video
$youtube_code = false;
if($video_id){
$youtube_code = '<object class="embed" width="'.$width.'" height="'.$height.'" type="application/x-shockwave-flash" data="http://www.youtube.com/v/'.$video_id.'"><param name="wmode" value="transparent"><param name="movie" value="http://www.youtube.com/v/'.$video_id.'" /><em>You need to a flashplayer enabled browser to view this YouTube video</em></object>';
}

$row->text = $youtube_code;

?>